﻿@using OShop.Extensions
@using OShop.Models
@using Orchard.ContentManagement

@{
    ShoppingCart cart = Model.Cart;
    var shipping = cart.Shipping as IPrice;
    var numberFormat = WorkContext.NumberFormat();
}

<div class="cart-actions-head">
    @Html.ActionLink(T("Empty cart").Text, "Empty", "ShoppingCart", new { Area = "OShop" }, new { })
</div>
<table class="cart-items">
    <thead>
        <tr>
            <th>@T("Product")</th>
            <th>@T("Unit Price")</th>
            <th>@T("Qty.")</th>
            <th>@T("Total")</th>
        </tr>
    </thead>
    @{
        int i = 0;
    }
    @foreach (var entry in cart.Items) {
        var stock = entry.Item.As<IStock>();
        int? maxQty = stock != null ? stock.MaxOrderQty : null;
        <tr>
            <td>
                @if (entry.Item.ContentItem != null) {
                    @Display(BuildDisplay(entry.Item, "Summary"))
                }
                else {
                    <div class="designation">
                        @entry.Item.Designation
                    </div>
                    if (!String.IsNullOrWhiteSpace(entry.Item.Description)) {
                        <div class="description">@entry.Item.Description</div>
                    }
                }
            </td>
            <td>
                @String.Format(numberFormat, "{0:C}", entry.Item.VatIncluded(entry.UnitPrice))
            </td>
            <td>
                <input type="hidden" name="@String.Format("CartItems[{0}].Id", i)" value="@entry.Id" />
                <input type="number" name="@String.Format("CartItems[{0}].Quantity", i)" value="@entry.Quantity" min="0" max="@maxQty" class="quantity oshop-autoupdate" />
                <button type="submit" name="@String.Format("CartItems[{0}].IsRemoved", i)" class="oshop-confirm" data-msg="@T("Are you sure you want to remove this item from your cart ?")" value="true">@T("Remove")</button>
            </td>
            <td>
                @String.Format(numberFormat, "{0:C}", entry.Item.VatIncluded(entry.SubTotal()))
            </td>
        </tr>
                i++;
    }
    <tfoot>
        @if (shipping != null) {
            <tr class="total">
                <td colspan="3">
                    @T("Items total")
                </td>
                <td>
                    @String.Format(numberFormat, "{0:C}", cart.ItemsTotal())
                </td>
            </tr>
            <tr class="total">
                <td colspan="3">
                    @T("Shipping")
                </td>
                <td>
                    @String.Format(numberFormat, "{0:C}", shipping.Price)
                </td>
            </tr>
        }
        @if (cart.Taxes.Any()) {
            <tr class="total">
                <td colspan="3">
                    @T("Taxes")
                </td>
                <td>
                    @String.Format(numberFormat, "{0:C}", cart.TaxesTotal())
                </td>
            </tr>
        }
        <tr class="total">
            <td colspan="3">
                @T("Total")
            </td>
            <td>
                @String.Format(numberFormat, "{0:C}", cart.CartTotal())
            </td>
        </tr>
    </tfoot>
</table>
